package com.fruit.service.lightpay.impl;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.fruit.constants.PayConstant;
import com.fruit.mapper.lightpay.LightPayMapper;
import com.fruit.pojo.lightpay.LightPayCashier;
import com.fruit.pojo.lightpay.LightPayCashierAlipay;
import com.fruit.service.lightpay.LightPayNotifyManager;
import com.fruit.service.pay.PayManager;
import com.fruit.util.CustomUtil;
import com.fruit.util.DataUtil;
import com.fruit.util.DateUtil;

@Service("lightPayNotifyService")
public class LightPayNotifyService implements LightPayNotifyManager{

	@Autowired
	private LightPayMapper lightPayMapper;
	
	@Autowired
	private PayManager payService;
	
	@Override
	public String updateLightPayCashier(LightPayCashierAlipay result) {

		String tradeStatus = result.getTradeStatus();
		String code = result.getOutTradeNo();
		//查询收银记录
		LightPayCashier lpc = lightPayMapper.getLightpayCashierByCode(code);
		if(lpc != null && PayConstant.State.notpay.equals(lpc.getState())){
			LightPayCashierAlipay lpca = lightPayMapper.getLightpayCashierAlipay(lpc.getId());
			BigDecimal totalAmount = DataUtil.getBigDecimal(result.getTotalAmount());
			if(lpca != null && DataUtil.getBigDecimal(lpca.getTotalAmount()).compareTo(totalAmount) == 0){
				String curTime = DateUtil.getTime();
				lpca.setTradeNo(result.getTradeNo());
				lpca.setBuyerId(result.getBuyerId());
				lpca.setTradeStatus(tradeStatus);
				lpca.setRefundFee(result.getRefundFee());
				lpca.setGmtCreate(result.getGmtCreate());
				lpca.setGmtPayment(result.getGmtPayment());
				lpca.setGmtRefund(result.getGmtRefund());
				lpca.setGmtClose(result.getGmtClose());
				lpca.setTimeEnd(curTime);
				lpc.setBackState(PayConstant.BackState.backok.getValue());
				if(StringUtils.equals(tradeStatus,LightPayCashierAlipay.TRADE_SUCCESS)
						|| StringUtils.equals(tradeStatus,LightPayCashierAlipay.TRADE_FINISHED)){
					lpc.setState(PayConstant.State.pay.getValue());
				}else{
					lpc.setState(PayConstant.State.faile.getValue());
				}
				lpc.setFinishTime(curTime);
				int num = lightPayMapper.updateLightpayStateById(lpc);
				if(num == 1){
					num = lightPayMapper.updateLightpayAlipay(lpca);
				}
				if(num == 0){
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return "faile";
				}
				if(num == 1 && PayConstant.State.pay.equals(lpc.getState())){
					final long lpcId = lpc.getId();
					final String transactionCode = lpc.getTransactionCode();
					//异步处理业务逻辑
					CustomUtil.threadPool.execute(new Runnable(){
						@Override
						public void run() {
							payService.updateTransaction(lpcId, transactionCode);
						}
					});
				}
				return "success";
			}
		}
			
		
		return "faile";
	}

	
	
	
	
}
